<?php

namespace app\admin\model;

use think\Model;
use think\Image;
use think\Db;
class File extends Model
{
        //多图片的上传
    function fileUpload($file)
    {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info) {
                $before = $info->getInfo();
                $res['oldname'] = $before['name'];
                $res['fileformat'] = $before['type'];
                $res['filesize'] = getRealSize($before['size']);
                $res['filepath'] = '/uploads/' . $info->getSaveName();//地址
            } else {
                $res['status'] = 0;
                $res['msg'] = '上传失败！' . $file->getError();
            }
            return $res;
    }



     // 图片上传
    function uploadImg($file,$movePath,$name=false,$type=''){
        if (empty($file)) {
            $returnData['status'] = 2;
            $returnData['msg'] = '上传文件不能为空';
        }else{
            if(count($file) > 1){
                $i = 0;
                $returnData['status'] = 1;
                $returnData['msg'] = lang('successfully');
                foreach ($file as $_file ) {
                    $returnData['data'][$i] = $this->uploadImgs($_file,$movePath,2);
                    $i++;
                }
            }else{
                $returnData = $this->uploadImgs($file,$movePath,1);
            }
        }
        return json_encode($returnData);
    }


    /**
    *   图片上传 
    *   $file   图片信息    
    *   $movePath 图片上传路径
    *   $method     1=单图片上传  
    **/
    function uploadImgs($file,$movePath,$name=false,$method=1){
        // return $file;
            $filePaths = ROOT_PATH . 'public' . DS . $movePath;
            if(!file_exists($filePaths)){
                mkdir($filePaths,777,true);
            }
            // $info = $file->validate(['size' => 1 * 1024 * 1024, 'ext' => 'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . $movePath);
            $info = $file->validate(['size' => 1 * 1024 * 1024, 'ext' => 'jpg,png,gif,jpeg'])->move($filePaths);
            if ($info) {
                $before = $info->getInfo();
                $res['oldname'] = $before['name'];
                $res['filepath'] = $movePath . $info->getSaveName();//地址
                if($method ==1){
                    $res['status'] = 1;
                    $res['msg'] = lang('successfully');
                }
            } else {
                if($method ==1){
                    $res['status'] = 0;
                    $res['msg'] = lang('failed') . $file->getError();
                }else{
                    $res['oldname'] = '';
                    $res['filepath'] = '';
                }
            }
        return $res;
    }

    /**
     *关于产品的图片，pro_img是管理图片原文件的（默认是1000x1000pix），
     *同时生成三个尺寸（48px,128px,256px）缩略图 图标，这三个尺寸主要是图标，需要加水印使用时不保护。他们的文件名是以pro_id为目录，
     *pro_id+尺寸为文件名的方式保存。
    **/
    function multiAndSingleUpload($file,$filePath,$proName,$procharName=0,$img_oldname=0,$drank=0)
    {   

        if(count($file) > 0){
            $data = [];
            foreach ($file as $k => $v) {
                if($file[$k]){
                    $filePaths = ROOT_PATH . 'public' . DS . 'uploads' . DS .$filePath;
                    $filePaths = str_replace("\\","/",$filePaths);
                    if(!file_exists($filePaths)){
                        mkdir($filePaths,777,true);
                    }
                    if($procharName > 0){
                        $fileName = 'prochar_'.$procharName;
                    }else{
                        $fileName = 'pro_'.$proName;
                    }
                    if($img_oldname){
                        $info = $file[$k]->move($filePaths.'/'.$fileName,$fileName.'_'.$img_oldname);
                        $data[$k]['drank'] = $img_oldname; 
                    }else{
                        $info = $file[$k]->move($filePaths.'/'.$fileName,$fileName.'_'.($drank+$k+1));
                        $data[$k]['drank'] = $drank+$k+1; 
                    }
                    
                    $data[$k]['info'] = $info; 
                    if($info){
                        $imgpath = 'uploads/'.$filePath.'/'.$fileName.'/'.$info->getSaveName();
                        $date_path = 'uploads/'.$filePath.'/'.$fileName;
                        if(!file_exists($date_path)){
                            mkdir($date_path,0777,true);
                        }
                        // 获取后缀名
                        $suffixName = pathinfo($info->getFilename(), PATHINFO_EXTENSION);

                        // 缩略图thumb(width, heigh)
                        $image = \think\Image::open($imgpath);

                        // 主图+三个缩略图一起新增数据库
                        $widthAndheight = array(48,128,256);
                        foreach ($widthAndheight as $k1 => $value) {
                            if($img_oldname){
                                $thumb_path  = $date_path.'/'.$fileName.'_'.$img_oldname.'_'.$value.'.'.$suffixName;
                            }else{
                                $thumb_path  = $date_path.'/'.$fileName.'_'.($drank+$k+1).'_'.$value.'.'.$suffixName;
                            }
                            // 生成缩略图
                            $aa = $image->thumb($value, $value)->save($thumb_path);
                            $data[$k]['thumb'][$k1+1]['width'] = $value;
                            $data[$k]['thumb'][$k1+1]['height'] = $value;
                            $data[$k]['thumb'][$k1+1]['img_url'] = '/'.$thumb_path;
                        }
                    }else{
                        // 上传失败获取错误信息
                        $res['status'] = 0;
                        $res['msg'] = lang('failed') . $file[$k]->getError();
                    }
                }
            }
            return $data;
        }
    }

}